package com.focusai.efairy.ui.activity.location;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import com.amap.api.maps.AMap;
import com.amap.api.maps.MapView;
import com.amap.api.maps.model.Marker;
import com.amap.api.maps.model.MarkerOptions;
import com.focusai.efairy.R;
import com.focusai.efairy.business.location.LocationModel;
import com.focusai.efairy.model.location.Location;
import com.focusai.efairy.ui.base.SwipeBackBaseActivity;
import com.focusai.efairy.ui.widget.PopWindowMapTypeView;
import com.focusai.efairy.utils.log.Log;

import java.util.ArrayList;
import java.util.List;


/**
 * Created by luo_guangwen on 2017/8/8.
 */

public class AMapLocationActivity extends SwipeBackBaseActivity implements LocationModel.ILocationUpdateCallback,
        AMap.OnMarkerClickListener, AMap.InfoWindowAdapter, AMap.OnInfoWindowClickListener {
    public static final String KEY_MAP_ITEMS = "key_map_items";
    protected MapView mMapView;
    protected AMap aMap;
    protected List<MarkerOptions> markerOptionsList = new ArrayList<>();
    protected List<Marker> markerList = new ArrayList<>();
    protected RecyclerView mRecyclerView;
    //    protected LocationModel mLocationModel;
    protected PopWindowMapTypeView mapTypeView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.act_base_location);
        initView();
        bindEvents();
        mMapView = findView(R.id.map_view);
        mMapView.onCreate(savedInstanceState);
        setDefaultValues();
        initMap();
    }

    @Override
    protected void initView() {
        mRecyclerView = findView(R.id.map_view_rcv);
    }

    @Override
    protected void setDefaultValues() {
        mRecyclerView.setLayoutManager(new LinearLayoutManager(getContext()));

//        mLocationModel = new LocationModel(getContext());
//        mLocationModel.register(this);
//        mLocationModel.startLocation();
    }

    public void setAdapter(RecyclerView.Adapter adapter) {
        mRecyclerView.setAdapter(adapter);
    }

    @Override
    protected void onResume() {
        super.onResume();
        //在activity执行onResume时执行mMapView.onResume ()，重新绘制加载地图
        mMapView.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
        //在activity执行onPause时执行mMapView.onPause ()，暂停地图的绘制
        mMapView.onPause();
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        //在activity执行onSaveInstanceState时执行mMapView.onSaveInstanceState (outState)，保存地图当前的状态
        mMapView.onSaveInstanceState(outState);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        //在activity执行onDestroy时执行mMapView.onDestroy()，销毁地图
        mMapView.onDestroy();
//        mLocationModel.unregister(this);

    }

    @Override
    public void locationSuccess(Location location) {
        if (location != null) {
            Log.D("Test", location.getAddress());
        }
    }

    @Override
    public void locationFail(int errorType) {
        Log.D("Test", "errorType = " + errorType);

    }

    /**
     * 初始化AMap对象
     */
    private void initMap() {
        if (aMap == null) {
            aMap = mMapView.getMap();
        }
        setUpMap();
    }

    private void setUpMap() {
        aMap.setOnMarkerClickListener(this);// 设置点击marker事件监听器
        aMap.setInfoWindowAdapter(this);//
        aMap.setOnInfoWindowClickListener(this);//
        addMarkersToMap();// 往地图上添加marker
    }

    protected void addMarkersToMap() {

    }

    @Override
    public boolean onMarkerClick(Marker marker) {
        marker.showInfoWindow();
        return false;
    }

    @Override
    public View getInfoWindow(Marker marker) {
        View view = getLayoutInflater().inflate(R.layout.custom_info_window, null);//display为自定义layout文件
        TextView titilText = (TextView) view.findViewById(R.id.tv_info_name);
        TextView addrText = (TextView) view.findViewById(R.id.tv_info_adress);
        titilText.setText(marker.getTitle());
        addrText.setText(marker.getSnippet());
        return view;
    }

    @Override
    public View getInfoContents(Marker marker) {
        return null;
    }

    @Override
    public void onInfoWindowClick(Marker marker) {

    }

    public void showMapType(double lat, double log, String adress) {
        if (mapTypeView == null) {
            mapTypeView = new PopWindowMapTypeView(getContext());
        }
        mapTypeView.show(mRecyclerView, lat, log, adress);
    }
}
